/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataStructure;

import static dataStructure.Sorting.quickSort;
import java.util.ArrayList;

/**
 *
 * @author mohamed.azouz
 */
public class Search {

    public static boolean bSearchRe(ArrayList<Integer> arr, int s, int e, int key) {
        if (s > e) {
            return false;
        }
        int m = (int) ((s + e) / 2);
        if (arr.get(m) == key) {
            return true;
        }
        if (key > arr.get(m)) {
            return bSearchRe(arr, s, m - 1, key);
        }
        if (key < arr.get(m)) {
            return bSearchRe(arr, m + 1, e, key);
        }
        return false;
    }

    public static boolean bSearchRe(ArrayList<Integer> arr, int key) {
        int s = 0;
        int e = arr.size();
        while (s <= e) {
            int m = (int) ((s + e) / 2);
            if (arr.get(m) == key) {
                return true;
            }
            if (key > arr.get(m)) {
                e = m - 1;
            }
            if (key < arr.get(m)) {
                s = m + 1;
            }
        }
        return false;
    }

    public static void main(String args[]) {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            int num = (int) (Math.random() * 100);
            arr.add(num);
        }
        System.out.println(arr);
        System.out.println(quickSort(arr));
        int key = arr.get(5);
        System.out.println("KEY = " + key);
        System.out.println(bSearchRe(arr, 0, arr.size(), key));
        System.out.println(bSearchRe(arr, key));
    }
}
